package widget.book.viewer
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Rectangle;
	
	import widget.book.manager.BookManager;
	
	public class PageViewer extends BasePageViewer{
		private var rightRect:Rectangle = null;
		private var leftRect:Rectangle = null;
		private var maskRect:Rectangle = null;
		
		private var bookManager:BookManager = null;
		public function PageViewer()
		{
			super();
			bookManager =  BookManager.getInstance();
			pageWidth = bookManager.bookModel.width / 2;
			pageHeight = bookManager.bookModel.height;
			
			pageBitmapData = new BitmapData(bookManager.bookModel.width, pageHeight);
			pageBitmap = new Bitmap();
			
			rightRect = new Rectangle(pageWidth, 0,pageWidth,pageHeight);
			leftRect = new Rectangle( 0, 0, pageWidth, pageHeight);
			maskRect = new Rectangle(0, 0, bookManager.bookModel.width, pageHeight);
		}
		
		protected override function completeHandler(event:Event):void {
			uLoader.scrollRect = maskRect;
			
			pageBitmapData.draw(uLoader);
			pageBitmap.bitmapData = pageBitmapData;
			pageBitmap.scrollRect = rightRect;
			addChild(pageBitmap);
			
			//dispatchEvent(new FlipEvent(FlipEvent.COMPLETE));
		}
		public override function set position(value:Number):void {
			switch (value) {
				case 0:
					pageBitmap.x = -pageWidth;
					pageBitmap.y = 0;
					pageBitmap.scrollRect = rightRect;
					break;
				case 1:
					pageBitmap.x = 0;
					pageBitmap.y = 0;
					pageBitmap.scrollRect = leftRect;
					break;
				case 2:
					pageBitmap.x = -pageWidth;
					pageBitmap.y = -pageHeight;
					pageBitmap.scrollRect = rightRect;
					break;
				case 3:
					pageBitmap.x = 0;
					pageBitmap.y = -pageHeight;
					pageBitmap.scrollRect = leftRect;
					break;
			}
		}
		public override function clone():BasePageViewer {
			var page:PageViewer = new PageViewer();
			var bmd:BitmapData = new BitmapData(bookManager.bookModel.width, pageHeight);
			bmd.draw(this.uLoader);
			page.pageBitmap.bitmapData = bmd;
			page.addChild(page.pageBitmap);
			
			return page;
		}
	}
}